{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "client: test\n"
     ]
    }
   ],
   "source": [
    "import MySQLdb\n",
    "import urllib2\n",
    "import sys\n",
    "import re\n",
    "import json\n",
    "\n",
    "def get_js_key(client_name):\n",
    "    conn = MySQLdb.connect(host='127.0.0.1', user='root', passwd='mypass', db='api')\n",
    "    cur = conn.cursor(MySQLdb.cursors.DictCursor)\n",
    "    sql = \"select * from consumer where short_name='{client_name}' and scope='js'\".format(**locals())\n",
    "    cur.execute(sql)\n",
    "    rows = cur.fetchall()\n",
    "    cur.close()\n",
    "    js_key = rows[0][\"consumer_key\"]\n",
    "    return js_key\n",
    "\n",
    "def format_result(raw_result):\n",
    "    raw_result = raw_result.strip()\n",
    "    m = re.search('\\{(.*)\\}',raw_result)\n",
    "    j = \"{\"+m.groups()[0]+\"}\"\n",
    "\n",
    "    d = json.loads(j)\n",
    "    for item in d[\"list\"]:\n",
    "        title = item[\"attributesName\"][\"title\"]\n",
    "        id = item[\"id\"]\n",
    "        print \"[{id}][{title}]\".format(**locals())\n",
    "        \n",
    "def get_recs(client_name):\n",
    "    js_key = get_js_key(client_name)\n",
    "    url = \"http://localhost:8080/js/recommendations?consumer_key={js_key}&user=u1&item=6023&limit=4&jsonpCallback=j\".format(**locals())\n",
    "    result = urllib2.urlopen(url).read()\n",
    "    \n",
    "    #print js_key\n",
    "    print \"--- api call ------------------------------\"\n",
    "    print \"\"\n",
    "    print url\n",
    "    print \"\"\n",
    "    print \"--- raw result ----------------------------\"\n",
    "    print \"\"\n",
    "    print result\n",
    "    print \"\"\n",
    "    print \"--- formatted result ----------------------\"\n",
    "    print \"\"\n",
    "    format_result(result)\n",
    "\n",
    "client_name = \"test\"\n",
    "print \"client: {client_name}\".format(**locals())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- api call ------------------------------\n",
      "\n",
      "http://localhost:8080/js/recommendations?consumer_key=19L73FWBMBFLSA62JNZC&user=u1&item=6023&limit=4&jsonpCallback=j\n",
      "\n",
      "--- raw result ----------------------------\n",
      "\n",
      "j({\"size\":4,\"requested\":4,\"list\":[{\"id\":\"21576\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"SIX KILLED IN SOUTH AFRICAN GOLD MINE ACCIDENT\",\"recommendationUuid\":\"1\"}},{\"id\":\"21575\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"SOVIET INDUSTRIAL GROWTH/TRADE SLOWER IN 1987\",\"recommendationUuid\":\"1\"}},{\"id\":\"21574\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"JAPAN/INDIA CONFERENCE CUTS GULF WAR RISK CHARGES\",\"recommendationUuid\":\"1\"}},{\"id\":\"21573\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"TOKYO DEALERS SEE DOLLAR POISED TO BREACH 140 YEN\",\"recommendationUuid\":\"1\"}}]})\n",
      "\n",
      "--- formatted result ----------------------\n",
      "\n",
      "[21576][SIX KILLED IN SOUTH AFRICAN GOLD MINE ACCIDENT]\n",
      "[21575][SOVIET INDUSTRIAL GROWTH/TRADE SLOWER IN 1987]\n",
      "[21574][JAPAN/INDIA CONFERENCE CUTS GULF WAR RISK CHARGES]\n",
      "[21573][TOKYO DEALERS SEE DOLLAR POISED TO BREACH 140 YEN]\n"
     ]
    }
   ],
   "source": [
    "get_recs(client_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--- api call ------------------------------\n",
      "\n",
      "http://localhost:8080/js/recommendations?consumer_key=19L73FWBMBFLSA62JNZC&user=u1&item=6023&limit=4&jsonpCallback=j\n",
      "\n",
      "--- raw result ----------------------------\n",
      "\n",
      "j({\"size\":4,\"requested\":4,\"list\":[{\"id\":\"17254\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS\",\"recommendationUuid\":\"2\"}},{\"id\":\"17289\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS\",\"recommendationUuid\":\"2\"}},{\"id\":\"3430\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"PICKENS SEES CONTINUED SLUMP IN WORKING RIGS\",\"recommendationUuid\":\"2\"}},{\"id\":\"17359\",\"name\":\"\",\"type\":1,\"first_action\":1450206435000,\"last_action\":1450206435000,\"popular\":false,\"demographics\":[],\"attributes\":{},\"attributesName\":{\"title\":\"DECLINE IN U.S. DOLLAR MAY BOOST OPEC OIL PRICE\",\"recommendationUuid\":\"2\"}}]})\n",
      "\n",
      "--- formatted result ----------------------\n",
      "\n",
      "[17254][WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS]\n",
      "[17289][WORLD OIL DEMAND LIKELY TO INCREASE, SUBROTO SAYS]\n",
      "[3430][PICKENS SEES CONTINUED SLUMP IN WORKING RIGS]\n",
      "[17359][DECLINE IN U.S. DOLLAR MAY BOOST OPEC OIL PRICE]\n"
     ]
    }
   ],
   "source": [
    "get_recs(client_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
